package com.uade.wcf.backend.domain.resources;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.uade.wcg.backend.DAO.EMFService;
import com.uade.wcg.backend.DAO.GroupDAO;
import com.uade.wcg.backend.domain.Group;
import com.uade.wcg.backend.domain.Team;
import com.uade.wcg.backend.services.GroupService;

public class GroupResource  extends ServerResource{
	
	List<Team> teams;
	//Group Name
	String group;
	
	@Get("json")
	public String handleGet() {
	try {
		this.group = (String) getRequestAttributes().get("group");
	    
		//Get Teams from domain by group
		this.teams = GroupService.getTeamsByGroupName(group);
		
	
		
	    JSONObject json = new JSONObject();
	    JSONArray jsonTeams = new JSONArray();
	   
	    
	    jsonTeams.put(teams);
	 	json.put("teams", jsonTeams);
	 	json.put("group", this.group);
		
	 	JsonRepresentation jsonRep = new JsonRepresentation(json);
	
		return jsonRep.getText();
	} catch (JSONException e) {
		e.printStackTrace();
	} catch (IOException e)
	{
		e.printStackTrace();
	}
	return null;
	}

}
